#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
 struct Node {
   int val;
   struct Node *next;
   struct Node *random;
 };

typedef struct Node Node;
struct Node* copyRandomList(struct Node* head) {
    if (head == NULL) {
        return head;
    }
    Node* src = head;
    while (src) {
        Node* ptr = (Node*)malloc(sizeof(Node));
        ptr->val = src->val;
        ptr->next = src->next;
        src->next = ptr;
        src = ptr->next;
    }
    src = head;
    while (src) {
        if (src->random == NULL) {
            src->next->random = NULL;
        }
        else {
            src->next->random = src->random->next;
        }
        src = src->next->next;
    }
    Node* newhead = head->next;
    src = newhead;
    while (src) {
        if (src->next != NULL) {
            src->next = src->next->next;
        }
        src = src->next;
    }
    return newhead;
}